package com.etc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Company;
import com.etc.entity.Jonseeker;
import com.etc.entity.User;
import com.etc.service.CompanyService;
import com.etc.service.JonseeService;
import com.etc.service.UserService;

@Controller
@SessionAttributes(value = "user")
public class UserController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private JonseeService jonseeService;
	@Autowired
	private CompanyService companyService;
	
	@PostMapping(value = "userLogin")
	@ModelAttribute(value="user")
	public ModelAndView judgeUser(HttpServletRequest request,@RequestParam(value="userNum") String userNum, @RequestParam(value="userPwd") String userPwd) {
		
		ModelAndView mv = new ModelAndView("");
		User user = new User(userNum, userPwd);
		user = userService.judgeUser(user);
		System.out.println("user:" + user);
		request.getSession().setAttribute("user", user);
		if(user == null) {
			mv.setViewName("userLogin");
		}else {
			if(user.getIsAdmin() == 2) {
				mv.setViewName("frame");
			}else {
				if(user.getIsAdmin() == 0) {
					//求职者登录，通过userId获取该求职者信息，页面判断如果信息为null，说明是以游客身份登录，到个人中心页面需要完善信息
					Jonseeker jonseeker = jonseeService.getJon(user.getUserId());
					System.out.println("jonseeker:" + jonseeker);
					request.getSession().setAttribute("jonseeker", jonseeker);
					mv.setViewName("personCenter");
				}else {
					Company company = companyService.getCompany(user.getUserId());
					System.out.println("company:" + company);
					request.getSession().setAttribute("company", company);
					mv.setViewName("companyCenter");
				}
				
			}
		}
		return mv;
		
	}
	
	@GetMapping("login")
	public String get() {
		
		System.out.println(111);
		return "userLogin";
	}
	
}